package com.zju.DoctorGo.controller;

import com.alipay.api.internal.util.AlipaySignature;
import com.zju.DoctorGo.config.AlipayConfig;
import com.zju.DoctorGo.dto.PaymentRequestDTO;
import com.zju.DoctorGo.service.PaymentService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;

@Controller
@CrossOrigin
@Slf4j
public class PayController {

    @Autowired
    private PaymentService paymentService;

    @RequestMapping("/api/pay/index")
    public String payIndex() {
        return "index";
    }

    @RequestMapping("/api/pay/showPay")
    @ResponseBody
    public void showPay(HttpServletRequest request, HttpServletResponse response) throws Exception {
        PaymentRequestDTO paymentRequest = new PaymentRequestDTO();
        paymentRequest.setWIDout_trade_no(new String(request.getParameter("WIDout_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        paymentRequest.setWIDtotal_amount(new String(request.getParameter("WIDtotal_amount").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        paymentRequest.setWIDsubject(new String(request.getParameter("WIDsubject").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        paymentRequest.setWIDbody(new String(request.getParameter("WIDbody").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        System.out.println(request.getParameter("WIDsubject"));
        System.out.println(paymentRequest);
        String result = paymentService.createPayment(paymentRequest);
        System.out.println("resp: " + result);
        response.getWriter().println(result);
    }
}
